<?php
/**
 * Created by PhpStorm.
 * User: huynguyen
 * Date: 9/22/14
 * Time: 8:19 PM
 */

namespace Backend\Modules\Video\Controllers;

use Phalcon\Paginator\Adapter\Model as Paginator;
use Phalcon\Mvc\Model\Resultset;
use Library\Pagination\Pager as Pager;
use Backend\Modules\Video\Models\Video;
use Backend\Modules\Video\Models\VideoGroup;
use Backend\Modules\Video\Models\VideoGroupLink;
use Backend\Modules\Video\Forms\FormCreateVideoGroup;

class VideoGroupController extends \BackendController
{
    public $urlVideoGroup = 'video/group';

    public function indexAction()
    {
        $currentPage = abs($this->request->getQuery('page', 'int', 1));
        $videoGroup = VideoGroup::find();
        $data = array();        

        if ($currentPage == 0) {
            $currentPage = 1;
        }
        $pager = new Pager(
            new Paginator(array(
                'data'  => $videoGroup,
                'limit' => 5,
                'page'  => $currentPage,
            )),
            array(
                'layoutClass' => 'Library\Pagination\Pager\Layout\Bootstrap',
                // Range window will be 5 pages
                'rangeLength' => 5,
                // Just a string with URL mask
                'urlMask'     => 'http://localhost/javhd/javhd-admin/video/national?page={%page_number}',
            )
        );

        $this->view->pager = $pager;
    }
    /*
     * creat group video
     */
    public function createAction()
    {
        $form = new FormCreateVideoGroup();

        if($this->request->isPost())
        {
            if($form->isValid($this->request->getPost()) != false)
            {
                $videoGroup = new VideoGroup();
                $videoGroup->assign(array(
                    'name' => $this->request->getPost('name'),
                    'description' => $this->request->getPost('description') ,
                    'total_episode' => $this->request->getPost('total_episode'),
                    'national_id'   => $this->request->getPost('national_id'),
                    'year_product'  => $this->request->getPost('year_product'),
                    'year_publish'  => $this->request->getPost('year_publish'),
                    'status'=> $this->request->getPost('status')
                ));

                if($videoGroup->save())
                {
                    return $this->response->redirect($this->getBackendUrl() . '/' . $this->urlVideoGroup);
                }
                $this->flash->error($videoGroup->getMessages());
            }
        }
        $this->view->form = $form;
    }

    public function editAction($id)
    {
        $this->assets
            ->addJs('vendor/backend/js/video/video_group.js');

        $videoGroup = VideoGroup::findFirstById($id);
        if(!$videoGroup)
        {
            $this->flash->error("Video group was not found");
            return $this->dispatcher->forward(array(
                'action' => 'index'
            ));
        }
        $form = new FormCreateVideoGroup($videoGroup , array('edit' => true));
        $form->setAction($this->getBackendUrl() . '/' . $this->urlVideoGroup . '/edit/' . $id);
        $tab = $this->request->get('tab') ? $this->request->get('tab') : 'general';

        if($this->request->getPost())
        {
            switch($tab)
            {
                case 'general':
                    if($form->isValid($this->request->getPost()) != false)
                    {
                        $videoGroup->assign(array(
                            'name' => $this->request->getPost('name'),
                            'description' => $this->request->getPost('description') ,
                            'total_episode' => $this->request->getPost('total_episode'),
                            'national_id'   => $this->request->getPost('national_id'),
                            'year_product'  => $this->request->getPost('year_product'),
                            'year_publish'  => $this->request->getPost('year_publish'),
                            'status'=> $this->request->getPost('status')
                        ));

                        if($videoGroup->save())
                        {
                            return $this->response->redirect($this->getBackendUrl() . '/' . $this->urlVideoGroup);
                        }
                        $this->flash->error($videoGroup->getMessages());
                    }
                    break;

                case 'video':
                    if($this->request->hasPost('selectVideo'))
                    {
                        $selectVideos = $this->request->getPost('selectVideo');
                        $data = array();
                        foreach($selectVideos as $videoId)
                        {
                            $checkVideoHaveVideoGroup = VideoGroupLink::find(array(
                                "video_id = $videoId AND video_group_id = $id"
                            ))->count();

                            if($checkVideoHaveVideoGroup <= 0)
                            {
                                $selectVideo = Video::findFirstById($videoId);
                                if($selectVideo)
                                {
                                    $videoGroupLink = new VideoGroupLink();
                                    $videoGroupLink->setVideoId($videoId);
                                    $videoGroupLink->setVideoGroupId($id);
                                    $videoGroupLink->save();
                                }
                            }
                        }
                    }

                    if($this->request->hasPost('selectedVideoRemove'))
                    {
                        $selectedVideos = $this->request->getPost('selectedVideoRemove');
                        $data = array();
                        foreach($selectedVideos as $videoId)
                        {
                            $videoGroupLink = VideoGroupLink::find(array(
                                "video_id = $videoId AND video_group_id = $id"
                            ))->getFirst();
                            if($videoGroupLink)
                            {
                                $videoGroupLink->delete();
                            }
                        }
                    }

                    break;
            }

        }
        $this->view->tab = $tab;
        $this->view->form = $form;
        $this->view->videoGroup = $videoGroup;
        $this->view->videoGroupLinks = VideoGroupLink::find(array(
            "video_group_id = $id"
        ));
    }

    /*
     * Delete video group
     */

    public function deleteAction($id)
    {
        $videoGroup = VideoGroup::findFirstById($id);
        if(!$videoGroup)
        {
            $this->flash->error("Video was not found");
            return $this->dispatcher->forward(array(
                'action' => 'index'
            ));
        }
        if(!$videoGroup->delete())
        {
            $this->flash->error($videoGroup->getMessages());
        }
        else
        {
            $this->flash->success("Video was deleted");
        }

        return $this->dispatcher->forward(array(
            'action' => 'index'
        ));
    }
}